unit UnitMedicos;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  DBGrids, sqldb, db, UnitMainForm;

type

  { TFormMedicos }

  TFormMedicos = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow();
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
  private
    { private declarations }
  public
    nombre : string;
  end; 

var
  FormMedicos: TFormMedicos;

implementation

uses
  UnitNuevoMedico, UnitModificarMedico;

{$R *.lfm}

{ TFormMedicos }

procedure TFormMedicos.ordenar_grid();
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from medico where mdc_nombre like ''%' + nombre + '%'' order by mdc_nombre';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Apellido y Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Matricula';

  DBGrid1.Columns[0].Width := 90;
  DBGrid1.Columns[1].Width := 230;
  DBGrid1.Columns[2].Width := 110;
end;

procedure TFormMedicos.FormShow();
begin
  nombre := '%';
  ordenar_grid();
end;

procedure TFormMedicos.MenuItem4Click(Sender: TObject);
begin
  FormNuevoMedico.ShowModal;

  SQLQuery1.Close;
  SQLQuery1.SQL.Text := 'select * from medico';
  SQLQuery1.Open;

  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  ordenar_grid();
end;

procedure TFormMedicos.MenuItem5Click(Sender: TObject);
begin
  ShowMessage('Click en el médico a modificar');
end;

procedure TFormMedicos.MenuItem6Click(Sender: TObject);
var
  codigo : string;

begin
  codigo := InputBox('Eliminar Medico', 'Ingrese el código del médico a eliminar', '');

  if codigo <> '' then
  begin
    if SQLQuery1.Active = true then
      SQLQuery1.Close;

      SQLQuery1.SQL.Text := 'delete from medico where mdc_cdg = ' + codigo;

      SQLQuery1.ExecSQL;

      SQLQuery1.SQL.Text := 'select * from medico';

      SQLQuery1.Open;

      SQLQuery1.ApplyUpdates;
      MainForm.SQLTransaction1.Commit;
      MainForm.ordenar_grid();

      ShowMessage('Baja Exitosa');

      ordenar_grid();
  end;
end;

procedure TFormMedicos.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro por Nombre', 'Ingrese parte del nombre del médico:', '');

  if nombre <> '' then
    ordenar_grid()
  else
    FormShow();
end;

procedure TFormMedicos.MenuItem9Click(Sender: TObject);
begin
  FormShow();
end;

procedure TFormMedicos.FormClose(Sender: TObject; var CloseAction: TCloseAction
  );
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TFormMedicos.DBGrid1CellClick(Column: TColumn);
begin
  FormModificarMedico.Label3.Caption := SQLQuery1.FieldByName('mdc_cdg').AsString;
  FormModificarMedico.Edit1.Text := SQLQuery1.FieldByName('mdc_nombre').AsString;
  FormModificarMedico.Edit2.Text := SQLQuery1.FieldByName('mdc_matricula').AsString;

  FormModificarMedico.ShowModal;

  SQLQuery1.Close;
  SQLQuery1.SQL.Text := 'select * from medico';
  SQLQuery1.Open;
  SQLQuery1.ApplyUpdates;
  MainForm.SQLTransaction1.Commit;
  MainForm.ordenar_grid();

  ordenar_grid();
end;

end.

